﻿/*
   Author: Tomer Paz
   BOGO.com
   2007
*/

/* some basic dialogs for Game Controller
   the idea is to have few basic structure that can be used
   to create flexible dialogs. like building blocks and/or Templates
   implemented as functions that return the desired GUI objects
*/
/* SimpleMsgBox - creates a simple message Box with text label, text box and two buttons */

package monopoly.GameController.Dialogs.Templates {
	
	import fl.controls.TextArea;
	import fl.controls.Button;
    import fl.controls.Label;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.EventDispatcher;
	import flash.text.TextFieldAutoSize;
	import fl.containers.ScrollPane;
	import monopoly.IFS.GLOBALS;
//	import flash.events.TextEvent;
	
	public class BasicDialog extends ScrollPane
		{
		public static const NO_CHOICE:Number = -1;
		protected var _mainPane:ScrollPane;
		protected var _slabel:Label;
		protected var _tArea:TextArea;
		protected var _b1:Button;
		protected var _b2:Button;
		protected var _ChosenOption:int;	// 1 = button 1, 2 = b2, -1 for none. Buttons are optional!
		
		protected var _defaultWidth = 180;
		protected var _defaultHeight = 255;
		protected var _defaultX		= 0;
		protected var _defaultY		= 0;
		
		/* storage Data for caller usage */
		
		protected var _data:Object;
		
		/* if Button2 is set to null, it will not be created */
		
		public function BasicDialog(LabelText:String, MsgText:String, Button1Text:String, Button2Text:String)
			{
			_ChosenOption = NO_CHOICE;	// no choice has been made	

			with (this)
				{
				setSize(_defaultWidth, _defaultHeight);
				x 	= 0;
				y	= 0;
				alpha = 0.8;
				//opaqueBackground = GLOBALS.DEFAULT_DIALOG_COLOR;
				//graphics.beginFill(GLOBALS.DEFAULT_DIALOG_COLOR, 0.8);
				//graphics.endFill();
				}
			
			setupLabel(LabelText);
			setupTextArea(MsgText);
			setupButtons(Button1Text, Button2Text);
			}
		
		public override function set width(value:Number):void
			{
			
			_tArea.setSize(value-GLOBALS.HPADDING*7, _defaultHeight/3);
			_tArea.drawNow();
			super.width = value;
			if (GLOBALS.DEBUG & GLOBALS.DEBUG_VIEW)
				{
				trace("[BasicDialog][width] tArea width is: " + _tArea.width);
				trace("[BasicDialog][width] BasicDialog width is: " + this.width);
				}
			}
		
		public function store_data(object:Object):void
			{
			_data = object;
			}
		public function retrieve_data():Object
			{
			return _data;
			}
			
		protected function setupLabel(tlabel:String):void
			{
			_slabel = new Label();
            _slabel.move(10, 5);
            _slabel.text = tlabel;
            _slabel.autoSize = TextFieldAutoSize.LEFT;
			addChild(_slabel);
			}
		protected function setupTextArea(txtMsg:String):void
			{
			_tArea = new TextArea();
			_tArea.setSize(_defaultWidth-20, _defaultHeight/3);
			_tArea.move(_slabel.x, _slabel.y+15);
			_tArea.text = txtMsg;
			_tArea.editable = false;
			addChild(_tArea);
			}
		protected function setupButtons(b1Txt:String, b2Txt:String):void
			{
			if (b1Txt != null)
				{
            	_b1 = new Button();
				_b1.move(10, _slabel.height + _tArea.height+15);
				//_b1.setSize(60, 30);
	            _b1.label = b1Txt;
				_b1.textField.autoSize = TextFieldAutoSize.LEFT;
	            _b1.addEventListener(MouseEvent.CLICK, buttonClick);
				addChild(_b1);
				}
			
			if (b2Txt != null)
				{
				_b2 = new Button();
				_b2.move(10, _slabel.height + _tArea.height+35);
				_b2.label = b2Txt;
				_b2.textField.autoSize = TextFieldAutoSize.LEFT;
	            _b2.addEventListener(MouseEvent.CLICK, buttonClick);
				addChild(_b2);
				}
	        }
		
		protected function buttonClick(e:MouseEvent):void 
			{
            switch(e.target) 
				{
                case _b1:
                    _ChosenOption = 1;
                    break;
                case _b2:
                    _ChosenOption = 2;
                    break;
                default:
					_ChosenOption = NO_CHOICE;
                    break;
				}
					
			/* inform Game Controller about the decision */
			
			if (NO_CHOICE != _ChosenOption)
				dispatchEvent(new Event(Event.CHANGE));
            }

		/* getter & setter (for Game Controller once it recieves the change event) */
		
		public function get ChosenOption():int
			{
			return _ChosenOption;
			}
		public function set ChosenOption(option:int):void
			{
			_ChosenOption = option;
			}
			
		/* to be called by the Game Controller once decision made (event sent from buttonClick) */
		
		public function remove( ):void
			{
			removeChild(_slabel);
			removeChild(_tArea);
			if (_b1)
				removeChild(_b1);
			if (_b2)
				removeChild(_b2);
			}
		}
}